package JasonsClasses;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ApplicationApplet extends JApplet implements MouseListener, ActionListener {

	private static int BOARDSIZE = 12;
	
	private AI Arty;
	private GameBoard board1, board2;
	private static int SMALL_SIZE = 20;
	private static int LARGE_SIZE = 30;
	private static int LARGE_X = 180;
	private static int LARGE_Y = 310;
	private static int SMALL_X = 450;
	private static int SMALL_Y = 20;
//define thread that will keep what has happened, on the screen, on the screen when covered
	private Thread drawingThread;
//define the variable stopped and set it to false when the program stops
	public boolean stopped = false;
//booleans that help determine whetehr your playing a 1 player game or a 2 player game
	public boolean oneplayergame = false;
	public boolean twoplayergame = false;
//define all necessary Buttons to be used on the pop up window
	private JFrame window;
	private LayoutManager layout;	
	private Canvas canvas;
//button for a 1 player game
	private JButton oneButton;
//button for a 2 player game
	private JButton twoButton;
//the button on the 1 player game to start the game after boats have been placed
	private JButton startButton;
//the button on the 2 player game to start the game after player 1's boats have been placed
	private JButton startoneButton;
//the button on the 2 player game to start the game after player 2's boats have been placed
	private JButton starttwoButton;
//button to display the instructions
	private JButton instructButton;
//button to return to the start screen from the instruction screen
	private JButton backButton;
//button to randomly place boats on the board on a one player game
	private JButton randomizeButton;
//button to randomly place boats on the board on a two player game for player 1
	private JButton randomizeoneButton;
//button to randomly place boats on the board on a two player game for player 2
	private JButton randomizetwoButton;
//button to individually place boats on the board on a one player game
	private JButton placeButton;
//button to individually place boats on the board on a two player game for player 1
	private JButton placeoneButton;
//button to individually place boats on the board on a two player game for player 2
	private JButton placetwoButton;
	
	//variable to help determine whose turn it is
	int player = 0;
//variables to help determine where the boxes are displayed on the screen
	int [][] Xstart = new int [12][12];
	int [][] Ystart = new int [12][12];
	int [][] Xend = new int [12][12];
	int [][] Yend = new int [12][12];
	
//helps determine where the boats are located on the board	
//[amount of boats][number of players][i coordinate of boat section][j coordinate of boat section]
	int [][][][] XcoorFourBoat = new int [1][2][12][12];
	int [][][][] XcoorThreeBoat = new int [2][2][12][12];
	int [][][][] XcoorTwoBoat = new int [3][2][12][12];
	int [][][][] XcoorOneBoat = new int [4][2][12][12];

//keeps track of which spaces are clicked
	int [][][] clicked = new int [2][12][12];
	//0 = unclicked
	//1 = clicked

	
//keeps track of what is happening in each space
	int [][][] spottype = new int [2][12][12];
	//0 = nothing
	//1 = edge
	//2 = boat
	//3 = nexttoaboat
	//4 = hit
	//5 = sunkboat
	//6 = clicked
	//7 = miss
	//method that is used in running the applet
	public void init()
	{
		
//initialize every spot on the board the unclicked and no activity, except for the edges
		board1 = new GameBoard(BOARDSIZE, BOARDSIZE);
		board2 = new GameBoard(BOARDSIZE, BOARDSIZE);
		Arty = new AI();
/*determines where each box is on the display, except for the edges		
		for(int i = 0; i < 12; i++){
			for(int j = 0; j < 12; j++){
				if((i != 0) && (i != 11) && (j != 0) && (j != 11)){
					Xstart[i][j] = (i*20) + 275; 
					Xend[i][j] = (i*20) + 275 + 20;
					Ystart[i][j] = (j-1)*20; 
					Yend[i][j] = (j-1)*20 + 20;
				}
				else{
					Xstart[i][j] = 0; 
					Xend[i][j] = 0;
					Ystart[i][j] = 0; 
					Yend[i][j] = 0;
				}
			}
		}*/
//creates the window, layout, canvas, and buttons (the instruction, 1 player, and 2 players buttons 
//being the only buttons visible at the start of the program)
				window = new JFrame("Battle Ship");
				layout = new FlowLayout();
				window.setLayout(layout);
				window.setSize(750,800);	
				canvas = new Canvas();
				canvas.setBackground(Color.WHITE);
				canvas.setSize(700, 700);
				canvas.addMouseListener(this);
				window.add(canvas);
				
				oneButton = new JButton("1 player");
				oneButton.addActionListener(this);
				window.add(oneButton);
				
				twoButton = new JButton("2 players");
				twoButton.addActionListener(this);
				window.add(twoButton);
				
				instructButton = new JButton("Instructions");
				instructButton.addActionListener(this);
				window.add(instructButton);
				
				startButton = new JButton("Start");
				startButton.addActionListener(this);
				window.add(startButton);
				startButton.setVisible(false);
				
				startoneButton = new JButton("Start");
				startoneButton.addActionListener(this);
				window.add(startoneButton);
				startoneButton.setVisible(false);
				
				starttwoButton = new JButton("Start");
				starttwoButton.addActionListener(this);
				window.add(starttwoButton);
				starttwoButton.setVisible(false);
				
				backButton = new JButton("Back");
				backButton.addActionListener(this);
				window.add(backButton);
				backButton.setVisible(false);
				
				randomizeButton = new JButton("Randomize");
				randomizeButton.addActionListener(this);
				window.add(randomizeButton);
				randomizeButton.setVisible(false);
				
				randomizeoneButton = new JButton("Randomize");
				randomizeoneButton.addActionListener(this);
				window.add(randomizeoneButton);
				randomizeoneButton.setVisible(false);
				
				randomizetwoButton = new JButton("Randomize");
				randomizetwoButton.addActionListener(this);
				window.add(randomizetwoButton);
				randomizetwoButton.setVisible(false);
				
				placeButton = new JButton("Place Boats");
				placeButton.addActionListener(this);
				window.add(placeButton);
				placeButton.setVisible(false);
				
				placeoneButton = new JButton("Place Boats");
				placeoneButton.addActionListener(this);
				window.add(placeoneButton);
				placeoneButton.setVisible(false);
				
				placetwoButton = new JButton("Place Boats");
				placetwoButton.addActionListener(this);
				window.add(placetwoButton);
				placetwoButton.setVisible(false);
				
				window.setVisible(true);
	}
	
	//paint on the canvas when placing boats
	public void paint(Graphics g, int k)
	{
//for every box on the display, color in the correct color for what the activity is on that box
		//Graphics g // = canvas.getGraphics();
		board1.paintBoard(LARGE_X, LARGE_Y, g, LARGE_SIZE);
		board2.paintBoard(SMALL_X, SMALL_Y, g, SMALL_SIZE);
		/*for(int i = 0; i < BOARDSIZE; i++){
			for(int j = 0; j < BOARDSIZE; j++){
//if there is no activity on the box
				if(spottype[k][i][j] == 0){
					g.setColor(Color.CYAN);
					g.fillRect(i*25, (j*25)+200, 25, 25);
				}
//if the box is an edge
				if(spottype[k][i][j] == 1){
					g.setColor(Color.WHITE);
					g.fillRect(i*25, (j*25)+200, 25, 25);
				}
//if the box has a boat in it
				if(spottype[k][i][j] == 2){
					g.setColor(Color.YELLOW);
					g.fillRect(i*25, (j*25)+200, 25, 25);
				}
//if the box has a boat next to it
				if(spottype[k][i][j] == 3){
					g.setColor(Color.BLUE);
					g.fillRect(i*25, (j*25)+200, 25, 25);
				}
//if the box has been clicked on and a boat was in it (might be unnessary for this function)
				if(spottype[k][i][j] == 4){
					g.setColor(Color.RED);
					g.fillRect(i*25, (j*25)+200, 25, 25);
				}
//if the boat has been sunk (might be unnessary for this function)
				if(spottype[k][i][j] == 5){
					g.setColor(Color.BLACK);
					g.fillRect(i*25, (j*25)+200, 25, 25);
				}
			}
		}
//draw the lines on the display to seperate each box from one another
		for(int i = 0; i < 12; i++){
			for(int j = 0; j < 12; j++){
				if((i != 0) && (i != 11) && (j != 0) && (j != 11)){
					g.setColor(Color.BLACK);
					g.drawRect(i*25, (j*25)+200, 25, 25);
				}
			}
		}
//draw numbers to determine which box is which
		g.setColor(Color.BLACK);
		g.drawString("1", 30, 220);
		g.drawString("2", 55, 220);
		g.drawString("3", 80, 220);
		g.drawString("4", 105, 220);
		g.drawString("5", 130, 220);
		g.drawString("6", 155, 220);
		g.drawString("7", 180, 220);
		g.drawString("8", 205, 220);
		g.drawString("9", 230, 220);
		g.drawString("10", 255, 220);
		g.drawString("1", 10, 240);
		g.drawString("2", 10, 265);
		g.drawString("3", 10, 290);
		g.drawString("4", 10, 315);
		g.drawString("5", 10, 340);
		g.drawString("6", 10, 365);
		g.drawString("7", 10, 390);
		g.drawString("8", 10, 415);
		g.drawString("9", 10, 440);
		g.drawString("10", 10, 465);
		*/
	}
	
	//when buttons are clicked
	public void actionPerformed(ActionEvent e) {
		Graphics g = canvas.getGraphics();
//when the back button is clicked, clear the screen, and re-display the start screen
//make the back button disappear and make the instruction, 1 player, and 2 players buttons reappear
		if(e.getSource() == backButton){
			g.setColor(Color.WHITE);
			g.fillRect(0, 0, 550, 600);
			oneButton.setVisible(true);
			twoButton.setVisible(true);
			instructButton.setVisible(true);
			backButton.setVisible(false);
			g.setColor(Color.BLACK);
			g.drawString("BATTLESHIP", 200, 250);
		}
//when the instruction button is clicked, clear the screen, and display the instruction of the game
//make the instruction, 1 player, and 2 players buttons disappear, and make the back button appear
		if(e.getSource() == instructButton){
			g.setColor(Color.WHITE);
			g.fillRect(0, 0, 550, 600);
			oneButton.setVisible(false);
			twoButton.setVisible(false);
			instructButton.setVisible(false);
			backButton.setVisible(true);
			g.setColor(Color.BLACK);
			g.drawString("Rules:\n\n", 0, 20);
			g.drawString("1. Players take turns attempting to get a 'hit' on one of their opponent's ships.", 0, 40);
			g.drawString("2. The player, in turn, clicks one square on his opponent's grid at which to shoot.", 0, 60);
			g.drawString("3. If the first selection is a hit, he or she may continue selecting coordinates until they miss.", 0, 80);
			g.drawString("When all the squares a ship is covering have been hit, that ship is considered sunk.\n", 15, 95);
			g.drawString("4. The first person to successfully sink all of his opponent's ships are placed wins the", 0, 115);
			g.drawString("game.", 15, 130);
		}
//when this button is clicked, make the instruction, 1 player, and 2 players buttons disappear, and make
//the randomize and place boats buttons appear, also clear screen and paint the empty board display so the 
//player can place boats also randomly place computers boats
		if(e.getSource() == oneButton){
			g.setColor(Color.WHITE);
			g.fillRect(0, 0, 550, 600);
			oneButton.setVisible(false);
			twoButton.setVisible(false);
			instructButton.setVisible(false);
			randomizeButton.setVisible(true);
			placeButton.setVisible(true);
			//randomlyplaceboats(1);
			repaint(0);
			g.setColor(Color.BLACK);
			g.drawString("Place your boats\n", 0, 10);
		}
//when this button is clicked, make the instruction, 1 player, and 2 players buttons disappear, and make
//the randomize and place boats buttons appear, also clear screen and paint the empty board display so 
//player 1 can place boats
		if(e.getSource() == twoButton){
			g.setColor(Color.WHITE);
			g.fillRect(0, 0, 550, 600);
			oneButton.setVisible(false);
			twoButton.setVisible(false);
			instructButton.setVisible(false);
			randomizeoneButton.setVisible(true);
			placeoneButton.setVisible(true);
			repaint(0);
			g.setColor(Color.BLACK);
			g.drawString("Player 1 place your boats\n", 0, 10);
		}
//when this button is clicked, make every button disappear, clear screen and display your board of boats
//and display your opponents empty clickable board
		if(e.getSource() == startButton){
			g.setColor(Color.WHITE);
			g.fillRect(0, 0, 550, 600);
			startButton.setVisible(false);
			randomizeButton.setVisible(false);
			placeButton.setVisible(false);
			//paintgameyou(0);
			//paintgameme(1);
			oneplayergame = true;
			//g.setColor(Color.BLACK);
			//g.drawString("Player 1's turn", 0, 10);
		}
//when this button is clicked, make every button disappear, make buttons randomize and place boats for 
//player 2 appear, also clear screen, and display player 2's empty board so they are able to place boats
		if(e.getSource() == startoneButton){
			g.setColor(Color.WHITE);
			g.fillRect(0, 0, 550, 600);
			startoneButton.setVisible(false);
			randomizeoneButton.setVisible(false);
			randomizetwoButton.setVisible(true);
			placeoneButton.setVisible(false);
			placetwoButton.setVisible(true);
			repaint(1);
			g.setColor(Color.BLACK);
			g.drawString("Player 2 place your boats\n", 0, 10);
		}
//when this button is clicked, make every button disappear, clear screen and display player 1's board of 
//boats and display player 2's empty clickable board
		if(e.getSource() == starttwoButton){
			g.setColor(Color.WHITE);
			g.fillRect(0, 0, 550, 600);
			starttwoButton.setVisible(false);
			randomizetwoButton.setVisible(false);
			placetwoButton.setVisible(false);
			//paintgameyou(0);
			//paintgameme(1);
			twoplayergame = true;
			//g.setColor(Color.BLACK);
			//g.drawString("Player 1's turn", 0, 10);
		}
//when this button is clicked, clear the screen, and redisplay the board with the boats randomly placed
		if(e.getSource() == randomizeButton){
			g.setColor(Color.WHITE);
			g.fillRect(0, 0, 550, 600);
			Arty.randomlyplaceboats(board1);
			//randomlyplaceboats(0);
			//paint(0);
			repaint(0);
			g.setColor(Color.BLACK);
			g.drawString("Place your boats\n", 0, 10);
			startButton.setVisible(true);

		}
//when this button is clicked, clear the screen, and redisplay the board with player 1's boats randomly 
//placed
		if(e.getSource() == randomizeoneButton){
			g.setColor(Color.WHITE);
			g.fillRect(0, 0, 550, 600);
			//randomlyplaceboats(0);
			repaint(0);
			g.setColor(Color.BLACK);
			g.drawString("Player 1 place your boats\n", 0, 10);
			startoneButton.setVisible(true);
		}
//when this button is clicked, clear the screen, and redisplay the board with player 2's boats randomly 
//placed
		if(e.getSource() == randomizetwoButton){
			g.setColor(Color.WHITE);
			g.fillRect(0, 0, 550, 600);
			//randomlyplaceboats(1);
			repaint(1);
			g.setColor(Color.BLACK);
			g.drawString("Player 2 place your boats\n", 0, 10);
			starttwoButton.setVisible(true);
		}
//when this button is clicked, clear the screen, and start the function to place boats, when thats done 
//then redisplay the board
		if(e.getSource() == placeButton){
			startButton.setVisible(true);
			g.setColor(Color.WHITE);
			g.fillRect(0, 0, 550, 600);
			//placeboats(0);
			repaint(0);
			g.setColor(Color.BLACK);
			g.drawString("Place your boats\n", 0, 10);
		}
//when this button is clicked, clear the screen, and start the function to place player 1's boats, when 
//thats done then redisplay player 1's board
		if(e.getSource() == placeoneButton){
			startoneButton.setVisible(true);
			g.setColor(Color.WHITE);
			g.fillRect(0, 0, 550, 600);
			//placeboats(0);
			repaint(0);
			g.setColor(Color.BLACK);
			g.drawString("Player 1 place your boats\n", 0, 10);
		}
//when this button is clicked, clear the screen, and start the function to place player 2's boats, when 
//thats done then redisplay player 2's board
		if(e.getSource() == placetwoButton){
			starttwoButton.setVisible(true);
			g.setColor(Color.WHITE);
			g.fillRect(0, 0, 550, 600);
			//placeboats(1);
			repaint(1);
			g.setColor(Color.BLACK);
			g.drawString("Player 2 place your boats\n", 0, 10);
		}
	}

	public void mouseClicked(MouseEvent e) {
		
	}

	public void mouseEntered(MouseEvent e) {
		
	}

	public void mouseExited(MouseEvent e) {
		
	}

	public void mousePressed(MouseEvent e) {
		
	}

	public void mouseReleased(MouseEvent e) {
		
	}
	
	
}
